package dao

import (
	"go_server/game_server/database/mysql"
	"go_server/game_server/util"
	"go_server/zinx/zlog"
	"gorm.io/gorm"
	"time"
)

type User struct {
	Uid string `gorm:"column:uid;primary"`
	Platform string `gorm:"column:platform"`
	Channel string `gorm:"column:channel"`
	RoleId int64 `gorm:"column:role_id"`
	CreateIp string `gorm:"column:create_ip"`
	CreateTime int64 `gorm:"column:create_time"`
}

func (User) TableName() string {
	return "user"
}

var(
	all_user_field = util.GetGormFields(User{})
)

func GetUserByUid(uid string) *User {
	db := mysql.GetServerDBConnection()
	var user User
	if err := db.Select(all_user_field).Where("uid=?", uid).First(&user).Error; err != nil {
		if err != gorm.ErrRecordNotFound {
			zlog.Errorf("GetUserByUid ERR!!! Id: %s, err: %v", uid, err)
		}
		return nil
	}

	return &user
}

func NewUserData(uid string, roleId int64, ip string) {
	user := &User{
		Uid: uid,
		Platform: "",
		Channel: "",
		RoleId: roleId,
		CreateIp: ip,
		CreateTime: time.Now().UnixMilli(),
	}
	db := mysql.GetServerDBConnection()
	if err := db.Create(user).Error; err != nil {
		zlog.Errorf("create User %v data failed: %v", user, err)
	}
}